package com.vtquang.mymaps.overlay;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.location.Address;
import android.location.Geocoder;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.vtquang.mymaps.R;
import com.vtquang.mymaps.R.drawable;

public class LocationOverlay extends MyOverlay {  
	private boolean touchStarted = false;
	protected Context _context; 
	protected GeoPoint _gp;
	protected Point _point;
	protected Bitmap _bmp;
	protected Rect rect; 

	public LocationOverlay(GeoPoint gp, Bitmap bmp) {
		_gp = gp;
		_bmp = bmp; 
	}
	
	public LocationOverlay(GeoPoint gp, Context context) {
		_gp = gp; 
		_context = context; 
		getBitmap();
	}
	
	protected void getBitmap(){
		Resources res = _context.getResources();
		_bmp = BitmapFactory.decodeResource(res, R.drawable.ic_pin_searchlocation);
	}

	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {
		super.draw(canvas, mapView, shadow, when);
		_point = new Point();
		mapView.getProjection().toPixels(_gp, _point);

		int left = (int) (_point.x - _bmp.getWidth() / 2);
		int top = (int) (_point.y - _bmp.getHeight());
		int right = left + _bmp.getWidth();
		int bottom = top + _bmp.getHeight();
		rect = new Rect(left, top, right, bottom); 

		// ---add the marker---
		canvas.drawBitmap(_bmp, left, top, null);
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent e, MapView mapView) {
		switch (e.getAction()) {
		case MotionEvent.ACTION_DOWN:
			touchStarted = true;
			break;
		case MotionEvent.ACTION_MOVE:
			touchStarted = false;
			break;
		case MotionEvent.ACTION_UP:
			if (touchStarted && isWrap(e.getX(), e.getY())) {
				ShowAddress(mapView);
			}
			break;
		}

		return super.onTouchEvent(e, mapView);
	}

	private void ShowAddress(MapView mapView) {
		Context context = mapView.getContext();
		Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
		String address = "";

		try {
			List<Address> addresses = geoCoder.getFromLocation(
					_gp.getLatitudeE6() / 1E6, _gp.getLongitudeE6() / 1E6, 1);
			if (addresses.size() > 0) {
				int count = addresses.get(0).getMaxAddressLineIndex();
				for (int i = 0; i < count; i++)
					address += addresses.get(0).getAddressLine(i)
							+ ((i < count - 1) ? ", " : "");
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		Toast.makeText(context, address, Toast.LENGTH_LONG).show();
		touchStarted = false;
	}

	private boolean isWrap(float x, float y) {
		return (rect != null && x >= rect.left && x <= rect.right
				&& y >= rect.top && y <= rect.bottom);
	}
	
	public static String getNameOverlay() {
		return "LocationOverlay";
	}
	
	@Override
	public String getName() {
		return getNameOverlay();
	}
}
